package com.lse_from.config.pg_data_type

import org.apache.ibatis.type.BaseTypeHandler
import org.apache.ibatis.type.JdbcType
import org.apache.ibatis.type.MappedTypes
import org.postgresql.util.PGobject
import java.sql.CallableStatement
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException

@MappedTypes(value = arrayOf(Any::class))
class JSONTypeHandlerPg : BaseTypeHandler<Any>() {
    private val jsonObject = PGobject()

    @Throws(SQLException::class)
    override fun setNonNullParameter(ps: PreparedStatement, i: Int, parameter: Any, jdbcType: JdbcType?) {
        jsonObject.type = "json"
        jsonObject.value = parameter.toString()
        ps.setObject(i, jsonObject)
    }

    @Throws(SQLException::class)
    override fun getNullableResult(rs: ResultSet, columnIndex: Int): Any? {
        return rs.getString(columnIndex)
    }

    @Throws(SQLException::class)
    override fun getNullableResult(cs: CallableStatement, columnIndex: Int): Any? {
        return cs.getString(columnIndex)
    }

    @Throws(SQLException::class)
    override fun getNullableResult(rs: ResultSet, columnName: String?): Any? {
        return rs.getString(columnName)
    }
}